<?php

//防止恶意调用
if (!defined('IN_MOD'))
{
    die('Hacking attempt');
}

// 添加到购物车
if($ajaxname == 'addcart')
{
	// 商品模块列表字段名配置
	$hdstatus_name = getdesignatedname("hdstatus");// 活动状态
	$integraldkstatus_name = getdesignatedname("integraldkstatus");// 积分抵扣状态
	
	$ajaxid = moduleaqvarval("ajaxid","intval");
	$columnid = moduleaqvarval("columnid","intval");
	$shopparameter = getshopattrparameter($columnid);
	foreach($shopparameter as $key=>$val)
	{
		$goodsattr[$val[$configinfo['article_id']]] = moduleaqvarval(array("postdata","goodsattr_".$val[$configinfo['article_id']].""),"string");
		if(empty($goodsattr[$val[$configinfo['article_id']]]))$goodsattrno = 1;
	}
	$goodsnumber = moduleaqvarval(array("postdata","goodsnumber"),"intval");
	if(empty($ajaxid))ajax_error(1,"请求数据不存在，非法操作！");
	if($goodsattrno == 1)ajax_error(1,"还有商品订购属性未选，请先选择！");
	if(empty($goodsnumber))ajax_error(1,"订购数量不合法，非法操作！");
	
	// 当前商品
	$modulewhere=array();
	$modulewhere["tablename"]="fastxcms_portal_post_product";
	$modulewhere["dataid"]=$ajaxid;
	$modulewhere["dataidname"]=$configinfo['article_id'];
	$goodsinfo=modulepara("datainfo",$modulewhere);
	if(empty($goodsinfo))ajax_error(1,"不存在商品，非法操作！");
	
	// 限制自己不可以购买自己店铺的商品
	if($goodsinfo['supplier_id'] == $configmember['id'])
	{
		ajax_error(1,"真调皮，不可以购买自己店铺的商品哦！");
	}
	
	// 当前店铺
	$supplierinfo = getsupplierinfo($goodsinfo['supplier_id']);
	
	// 购物车中的兑换商品与普通商品不可同时存在处理
	$integralstatus_name = getdesignatedname("integralstatus");
	if($goodsinfo[$integralstatus_name] == 1)
	{
		// 当为积分兑换商品时清空购物车中的所有商品
		deletedata("modulepara_cart", "userid", $configmember['id']);
	}else{
		// 当为普通商品时清空购物车中的积分兑换商品
		deletedata("modulepara_cart", "userid", $configmember['id'], "integralgoods = 1");		
	}
	
	// 获取购买商品属性
	$goodsattrinfo = '';
	foreach($goodsattr as $key => $val)
	{
		$modulewhere=array();
		$modulewhere["tablename"]=$configinfo['ecs_article'];
		$modulewhere["dataid"]=$key;
		$modulewhere["dataidname"]=$configinfo['article_id'];
		$attrinfo=modulepara("datainfo",$modulewhere);
		$goodsattrinfo .= $attrinfo[$configinfo['title']]."-".$val.",";
	}
	$goodsattrinfo = delstrh($goodsattrinfo);
	
	// 获取购买商品属性规格信息（价格，库存，货号等）
	$specinfoidarr = array();
	foreach($goodsattr as $key => $val)
	{
		$modulewhere=array();
		$modulewhere["tablename"]=$configinfo['ecs_article'];
		$modulewhere["dataid"]=$val;
		$modulewhere["dataidname"]=$configinfo['title'];
		$modulewhere["where"]=$configinfo['article_cat_id']."=".$configinfo['shopparalistmodule'];
		$specinfo=modulepara("datainfo",$modulewhere);
		$specinfoidarr[] = $specinfo[$configinfo['article_id']];
	}
	$specinfoids = getarrtostr('-',$specinfoidarr);
	
	// 订购规格参数获取
	$parameterinfo = getmoduleparameter($ajaxid, 3);
	$shopdatalist=getshopattrparameter($columnid);
	$goodsspecinfo = getordergoodsspecinfo($parameterinfo['shoppara_goodsparalist'],$shopdatalist);
	$goodsspecinfo_priceson = $goodsspecinfo["priceson"][$specinfoids];// 价格
	$goodsspecinfo_countson = $goodsspecinfo["countson"][$specinfoids];// 库存
	$goodsspecinfo_numberson = $goodsspecinfo["numberson"][$specinfoids];// 货号
	$goodsspecinfo_integralson = $goodsspecinfo["integralson"][$specinfoids];// 积分
	
	// 计算商品本身优惠折扣后价格（会员等级或促销价格等）
	$goodsprice = $goodsspecinfo_priceson;
	$price = getgoodsdiscountprice($goodsspecinfo_priceson,$goodsinfo['id']);
	
	// 当前购物车中商品为积分兑换商品时使用权限判断
	if($goodsinfo[$integralstatus_name] == 1)
	{
		// 获取商品积分兑换价格
		$integralprice = $goodsspecinfo_integralson;
		
		// 判断会员积分是否足够
		if($configmember['score'] < $integralprice)
		{
			ajax_error(1,"您的积分不足，无法订购！当前积分：".$configmember['score']);	
		}
	}
	
	// 商品货号
	$goods_sn = $goodsspecinfo_numberson;
	
	// 库存判断
	$kucun = $goodsspecinfo_countson;
	if(empty($kucun))ajax_error(1,"库存数量不足，无法订购！");
	
	// 检查购物车是否存在相同属性
	$modulewhere=array();
	$modulewhere["tablename"]="modulepara_cart";
	$modulewhere["dataid"]=$configmember['id'];
	$modulewhere["dataidname"]="userid";
	$modulewhere["where"]="goods_attr = '".$goodsattrinfo."'";
	$attrgoods=modulepara("datainfo",$modulewhere);
	if(is_array($attrgoods))
	{
		$moduledata = array();
		$moduledata['tablename'] = "modulepara_cart";
		$moduledata['data'] = array('number' => $attrgoods['number'] + $goodsnumber);
		$moduledata['dataidname'] = 'userid';
		$moduledata['dataid'] = $configmember['id'];
		$moduledata['where'] = "id = '".$attrgoods['id']."'";
		modulepara("updatedata",$moduledata);	
	}else{
		$moduledata = array();
		$moduledata['listid'] = $ajaxid;
		$moduledata['listcatid'] = $columnid;
		$moduledata['userid'] = $configmember['id'];
		$moduledata['goods_name'] = $goodsinfo[$configinfo['title']];
		$moduledata['goods_img'] = $goodsinfo[$configinfo['thumb']];
		$moduledata['price'] = $price;
		$moduledata['goodsprice'] = $goodsprice;
		$moduledata['number'] = $goodsnumber;
		$moduledata['goods_sn'] = $goods_sn;
		$moduledata['goods_attr'] = $goodsattrinfo;
		if($goodsinfo[$hdstatus_name])$moduledata['hdstatus'] = $goodsinfo[$hdstatus_name];
		$moduledata['giveintegral'] = $parameterinfo['giveintegral'];// 赠送积分
		if($goodsinfo[$integraldkstatus_name] == 1)$moduledata['integraldkmoney'] = $parameterinfo['integraldkprice'];// 积分抵扣价格
		if($goodsinfo[$integralstatus_name] == 1)$moduledata['integralprice'] = $integralprice;// 积分兑换数量
		if($goodsinfo[$integralstatus_name] == 1)$moduledata['integralgoods'] = 1;// 是否积分兑换商品
		$moduledata['supplier_id'] = $supplierinfo['id'];
		$moduledata['shop_name'] = $supplierinfo['shop_name'];
		$moduledata['lang'] = $configinfo['lang'];
		$insert_id = insertdata('modulepara_cart',$moduledata);
	}
	
	$cartnumber = getshopcartnumber();// 购物车数量
	$content['cartnumber'] = $cartnumber;
	
	ajax_success(0,"添加购物车成功",$content);
}

// 删除购物车商品
if($ajaxname == 'delcart')
{
	$ajaxid = moduleaqvarval("ajaxid","intval");
	$ids = moduleaqvarval("ids","string");
	if(empty($ajaxid) && empty($ids))ajax_error(1,"请求数据不存在，非法操作！");
	
	if($ids)
	{
		$idsgroup = getstrtoarr(',',$ids);
		foreach($idsgroup as $key=>$id)
		{
			deletedata("modulepara_cart", "id", $id, "userid = '".$configmember['id']."'");	
		}
	}else{
		deletedata("modulepara_cart", "id", $ajaxid, "userid = '".$configmember['id']."'");
	}
	
	$cartnumber = getshopcartnumber();// 购物车数量
	$content['cartnumber'] = $cartnumber;
	
	ajax_success(0,"删除购物车商品成功",$content);
}

// 更新购物车数量
if($ajaxname == 'cartnumber')
{
	$ajaxid = moduleaqvarval("ajaxid","intval");
	$number = moduleaqvarval("number","intval");
	if(empty($ajaxid))ajax_error(1,"请求数据不存在，非法操作！");
	if(empty($number))ajax_error(1,"订购数量不合法，非法操作！");
	
	$moduledata = array();
	$moduledata['tablename'] = "modulepara_cart";
	$moduledata['data'] = array('number' => $number);
	$moduledata['dataidname'] = 'userid';
	$moduledata['dataid'] = $configmember['id'];
	$moduledata['where'] = "id = '".$ajaxid."'";
	modulepara("updatedata",$moduledata);	
	
	$cartnumber = getshopcartnumber();// 购物车数量
	$content['cartnumber'] = $cartnumber;
	
	ajax_success(0,"更新购物车成功",$content);
}

// 添加优惠券
if($ajaxname == 'addyouhuijuan')
{	
	$hdtype_name = getdesignatedname("hdtype");

	$tomoney_name = getdesignatedname("tomoney");
	$hdgoodsids_name = getdesignatedname("hdgoodsids");
	$endtime_name = getdesignatedname("datetype_endtime");
	
	$ajaxid = moduleaqvarval("ajaxid","intval");
	
	// 当前优惠券信息
	$modulewhere=array();
	$modulewhere["tablename"]="fastxcms_portal_post_coupon";
	$modulewhere["dataid"]=$ajaxid;
	$modulewhere["dataidname"]=$configinfo['article_id'];
	$modulewhere["where"]="$endtime_name >= ".time();
	$yhjinfo=modulepara("datainfo",$modulewhere);
	
	// 限制自己不可以领取自己店铺的优惠券
	if($yhjinfo['supplier_id'] == $configmember['id'])
	{
		ajax_error(1,"真调皮，不可以领取自己店铺的优惠券哦！");
	}
	
	$parameterinfo = getmoduleparameter($yhjinfo['id'], 136);
	if($parameterinfo)$yhjinfo += $parameterinfo;
	if(!empty($yhjinfo))
	{
		// 领取优惠券插入数据
		if(empty($configmember['couponlist'][$yhjinfo['id']]))
		{
			$moduledata = array();
			$moduledata['name'] = $yhjinfo['post_title'];
			$moduledata['yhjid'] = $yhjinfo['id'];
			$moduledata['goodsids'] = $yhjinfo[$hdgoodsids_name];
			$moduledata['userid'] = $configmember['id'];
			$moduledata['tomoney'] = $yhjinfo[$tomoney_name];
			$moduledata['yhmoney'] = $yhjinfo['yhmoney'];
			$moduledata['hdtype'] = $yhjinfo[$hdtype_name];
			$moduledata['endtime'] = $yhjinfo[$endtime_name];
			$moduledata['supplier_id'] = $yhjinfo['supplier_id'];
			$insert_id = insertdata('fastxcms_user_coupon',$moduledata);
		}else{
			ajax_error(1,"您已领取优惠券");	
		}
	}
	
	ajax_success(0,"领取优惠券成功",$content);
}

// 添加收货人信息
if($ajaxname == 'addcontact')
{
	$ajaxid = moduleaqvarval("ajaxid","intval");
	$name = moduleaqvarval(array("postdata","name"),"string");
	$tel = moduleaqvarval(array("postdata","tel"),"string");
	$address = moduleaqvarval(array("postdata","address"),"string");
	$defok = moduleaqvarval(array("postdata","defok"),"intval");
	if(empty($name))ajax_error(1,"姓名不能为空");
	if(empty($tel))ajax_error(1,"电话不能为空");
	if(empty($address))ajax_error(1,"地址不能为空");
	
	if(!empty($ajaxid))
	{
		// 清空所有默认地址
		if($defok > 0)
		{
			$moduledata = array();
			$moduledata['tablename'] = "modulepara_address";
			$moduledata['data'] = array('defok' => 0);
			$moduledata['dataidname'] = 'userid';
			$moduledata['dataid'] = $configmember['id'];
			modulepara("updatedata",$moduledata);	
		}
		
		$tip = "修改收货人信息成功";
		$moduledata = array();
		$moduledata['tablename'] = "modulepara_address";
		$moduledata['data'] = array('name' => $name, 'tel' => $tel, 'address' => $address, 'defok' => $defok);
		$moduledata['dataidname'] = 'id';
		$moduledata['dataid'] = $ajaxid;
		$moduledata['where'] = "userid = '".$configmember['id']."'";
		modulepara("updatedata",$moduledata);	
		$insert_id = $ajaxid;
	}else{
		// 清空所有默认地址
		if($defok > 0)
		{
			$moduledata = array();
			$moduledata['tablename'] = "modulepara_address";
			$moduledata['data'] = array('defok' => 0);
			$moduledata['dataidname'] = 'userid';
			$moduledata['dataid'] = $configmember['id'];
			modulepara("updatedata",$moduledata);	
		}
		
		$tip = "添加收货人信息成功";
		$moduledata = array();
		$moduledata['userid'] = $configmember['id'];
		$moduledata['name'] = $name;
		$moduledata['tel'] = $tel;
		$moduledata['address'] = $address;
		$moduledata['defok'] = $defok;
		$insert_id = insertdata('modulepara_address',$moduledata);
	}
	
	$modulewhere=array();
	$modulewhere["tablename"]="modulepara_address";
	$modulewhere["dataid"]=$insert_id;
	$modulewhere["dataidname"]="id";
	$addressdata=modulepara("datainfo",$modulewhere);	
	
	ajax_success(0,$tip,$addressdata);
}

// 删除收货人信息
if($ajaxname == 'delcontact')
{
	$ajaxid = moduleaqvarval("ajaxid","intval");
	if(empty($ajaxid))ajax_error(1,"请求数据不存在，非法操作！");
	
	deletedata("modulepara_address", "id", $ajaxid, "userid = '".$configmember['id']."'");
	
	ajax_success(0,"删除收货信息成功",$ajaxid);
}

// 默认收货人信息
if($ajaxname == 'defcontact')
{
	$ajaxid = moduleaqvarval("ajaxid","intval");
	if(empty($ajaxid))ajax_error(1,"请求数据不存在，非法操作！");
	
	// 清空所有当前选择
	$moduledata = array();
	$moduledata['tablename'] = "modulepara_address";
	$moduledata['data'] = array('defok' => 0);
	$moduledata['dataidname'] = 'userid';
	$moduledata['dataid'] = $configmember['id'];
	modulepara("updatedata",$moduledata);	
	
	$moduledata = array();
	$moduledata['tablename'] = "modulepara_address";
	$moduledata['data'] = array('defok' => 1);
	$moduledata['dataidname'] = 'id';
	$moduledata['dataid'] = $ajaxid;
	$moduledata['where'] = "userid = '".$configmember['id']."'";
	modulepara("updatedata",$moduledata);	
	
	ajax_success();
}

// 订单状态操作
if($ajaxname == 'orderstatus')
{
	$status_name = getdesignatedname("orderstatus");
	
	$ajaxid = moduleaqvarval('ajaxid', 'intval');
	$status = moduleaqvarval('status', 'intval');
	
	// 当前订单信息
	$modulewhere=array();
	$modulewhere["tablename"]="fastxcms_portal_post_order";
	$modulewhere["dataid"]=$configmember['id'];
	$modulewhere["dataidname"]="user_id";
	$modulewhere["where"]="id = '".$ajaxid."'";
	$orderinfo=modulepara("datainfo",$modulewhere);
		
	// 取消订单
	if($orderinfo[$status_name] == 0 && $status == 5)
	{	
		$modulewhere=array();
		$modulewhere["tablename"]="fastxcms_portal_post_order";
		$modulewhere['data'] = array("$status_name"=>5);
		$modulewhere["dataidname"]="id";
		$modulewhere['where'] = "user_id='".$configmember['id']."'";
		$modulewhere['moduleid'] = 109;
		$result = ajax_updatemodule($modulewhere);
		if($result['error'] == 1)$this->error("不可操作其它模块下的数据或当前模块操作数据不存在，非法操作！");
		
		$this->success("取消订单成功！");
	}
	
	// 确认收货
	if($orderinfo[$status_name] == 2 && $status == 3)
	{	
		$modulewhere=array();
		$modulewhere["tablename"]="fastxcms_portal_post_order";
		$modulewhere['data'] = array("$status_name"=>3);
		$modulewhere["dataidname"]="id";
		$modulewhere['where'] = "user_id='".$configmember['id']."'";
		$modulewhere['moduleid'] = 109;
		$result = ajax_updatemodule($modulewhere);
		if($result['error'] == 1)$this->error("不可操作其它模块下的数据或当前模块操作数据不存在，非法操作！");
	
		$this->success("确认收货成功！");
	}
	
	$this->error("设置订单状态不符合，非法操作！");
}
?>